package com.ztom.daily.n2022.m3;

/**
 * 考试的最大困扰度
 * <p>
 * https://leetcode-cn.com/problems/maximize-the-confusion-of-an-exam/
 *
 * @author ZhangTao
 */
public class Day29MaxConsecutiveAnswers {

    public int maxConsecutiveAnswers(String answerKey, int k) {
        return Math.max(getMaxCount(answerKey, k, 'T'), getMaxCount(answerKey, k, 'F'));
    }

    private int getMaxCount(String str, int k, char t) {
        int res = 0;
        // 记录当前窗口内, 非 t 字符的个数
        int sum = 0;
        for (int l = 0, r = 0; r < str.length(); r++) {
            if (str.charAt(r) != t) {
                sum++;
            }
            // 非 t 字符超过 k时
            while (sum > k) {
                if (str.charAt(l) != t) {
                    sum--;
                }
                l++;
            }
            res = Math.max(res, r - l + 1);
        }
        return res;
    }
}
